﻿namespace Pub.Class
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using System.Runtime.InteropServices;

    public class Data
    {
        protected static string connString = null;
        protected static string dbType = null;
        private static DbProviderFactory factory = null;
        protected static string key = "ConnString";
        private static readonly object lockHelper = new object();
        private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
        private static readonly ISafeDictionary<string, Database> pool = new SafeDictionarySlim<string, Database>();
        private static readonly IList<string> poolkey = new List<string>();
        private static IDbProvider provider = null;
        private static long queryCount = 0L;

        private static string accessType = null;   //访问类别,w:web服务端,c:client客户
        private static string pwKey = null;        //加解密key

        public static void AddPool(Database db)
        {
            if (!db.IsNull())
            {
                AddPool(db.Pool, db);
            }
        }

        public static void AddPool(string key, Database db = null)
        {
            if (!key.IsNullEmpty())
            {
                key = key.ToLower();
                if (!pool.ContainsKey(key))
                {
                    if (db.IsNull())
                    {
                        Database database = new Database(key)
                        {
                            Pool = key
                        };
                        pool[key] = database;
                        poolkey.Add(key);
                    }
                    else
                    {
                        pool[key] = db;
                        poolkey.Add(key);
                    }
                }
            }
        }

        private static void AssignParameterValues(DbParameter[] commandParameters, DataRow dataRow)
        {
            if (!commandParameters.IsNull() && !dataRow.IsNull())
            {
                int num = 0;
                foreach (DbParameter parameter in commandParameters)
                {
                    if (parameter.ParameterName.IsNull() || (parameter.ParameterName.Length <= 1))
                    {
                        throw new Exception(string.Format("请提供参数{0}一个有效的名称{1}.", num, parameter.ParameterName));
                    }
                    if (dataRow.Table.Columns.IndexOf(parameter.ParameterName.Substring(1)) != -1)
                    {
                        parameter.Value = dataRow[parameter.ParameterName.Substring(1)];
                    }
                    num++;
                }
            }
        }

        private static void AssignParameterValues(DbParameter[] commandParameters, object[] parameterValues)
        {
            if (!commandParameters.IsNull() && !parameterValues.IsNull())
            {
                if (commandParameters.Length != parameterValues.Length)
                {
                    throw new ArgumentException("参数值个数与参数不匹配.");
                }
                int index = 0;
                int length = commandParameters.Length;
                while (index < length)
                {
                    if (parameterValues[index] is IDbDataParameter)
                    {
                        IDbDataParameter parameter = (IDbDataParameter)parameterValues[index];
                        if (parameter.Value.IsNull())
                        {
                            commandParameters[index].Value = DBNull.Value;
                        }
                        else
                        {
                            commandParameters[index].Value = parameter.Value;
                        }
                    }
                    else if (parameterValues[index].IsNull())
                    {
                        commandParameters[index].Value = DBNull.Value;
                    }
                    else
                    {
                        commandParameters[index].Value = parameterValues[index];
                    }
                    index++;
                }
            }
        }

        private static void AttachParameters(DbCommand command, DbParameter[] commandParameters)
        {
            if (command.IsNull())
            {
                throw new ArgumentNullException("command");
            }
            if (commandParameters.IsNotNull())
            {
                foreach (DbParameter parameter in commandParameters)
                {
                    if (parameter.IsNotNull())
                    {
                        if (((parameter.Direction == ParameterDirection.InputOutput) || (parameter.Direction == ParameterDirection.Input)) && parameter.Value.IsNull())
                        {
                            parameter.Value = DBNull.Value;
                        }
                        command.Parameters.Add(parameter);
                    }
                }
            }
        }

        public static void CacheParameterSet(string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            string str = ConnString + ":" + commandText;
            paramCache[str] = commandParameters;
        }

        private static DbParameter[] CloneParameters(DbParameter[] originalParameters)
        {
            DbParameter[] parameterArray = new DbParameter[originalParameters.Length];
            int index = 0;
            int length = originalParameters.Length;
            while (index < length)
            {
                parameterArray[index] = (DbParameter)((ICloneable)originalParameters[index]).Clone();
                index++;
            }
            return parameterArray;
        }

        public static void ClosePool(string key = null)
        {
            if (key.IsNullEmpty())
            {
                pool.Clear();
                poolkey.Clear();
            }
            else
            {
                key = key.ToLower();
                if (pool.ContainsKey(key))
                {
                    pool[key].ResetDbProvider();
                    pool[key] = null;
                    poolkey.Remove(key);
                    pool.Remove(key);
                }
            }
        }

        public static DbCommand CreateCommand(DbConnection connection, string spName, params string[] sourceColumns)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            DbCommand command = Factory.CreateCommand();
            command.CommandText = spName;
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            if (sourceColumns.IsNotNull() && (sourceColumns.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                for (int i = 0; i < sourceColumns.Length; i++)
                {
                    spParameterSet[i].SourceColumn = sourceColumns[i];
                }
                AttachParameters(command, spParameterSet);
            }
            return command;
        }

        private static DbParameter[] DiscoverSpParameterSet(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            DbCommand cmd = connection.CreateCommand();
            cmd.CommandText = spName;
            cmd.CommandType = CommandType.StoredProcedure;
            connection.Open();
            Provider.DeriveParameters(cmd);
            connection.Close();
            if (!includeReturnValueParameter)
            {
                cmd.Parameters.RemoveAt(0);
            }
            DbParameter[] array = new DbParameter[cmd.Parameters.Count];
            cmd.Parameters.CopyTo(array, 0);
            foreach (DbParameter parameter in array)
            {
                parameter.Value = DBNull.Value;
            }
            return array;
        }

        public static int ExecSql(string commandText)
        {
            return ExecSql((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public static int ExecSql(CommandType commandType, string commandText)
        {
            return ExecSql(commandType, commandText, null);
        }

        public static int ExecSql(string commandText, params DbParameter[] commandParameters)
        {
            return ExecSql((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public static int ExecSql(out int id, string commandText)
        {
            return ExecSql(out id, (commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, (DbParameter[])null);
        }

        public static int ExecSql(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return ExecSql(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(CommandType.StoredProcedure, spName);
        }

        public static int ExecSql(out int id, CommandType commandType, string commandText)
        {
            return ExecSql(out id, commandType, commandText, (DbParameter[])null);
        }

        public static int ExecSql(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                return ExecSql(connection, commandType, commandText, commandParameters);
            }
        }

        public static int ExecSql(DbConnection connection, CommandType commandType, string commandText)
        {
            return ExecSql(connection, commandType, commandText, null);
        }

        public static int ExecSql(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return ExecSql(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(connection, CommandType.StoredProcedure, spName);
        }

        public static int ExecSql(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return ExecSql(transaction, commandType, commandText, null);
        }

        public static int ExecSql(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return ExecSql(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(transaction, CommandType.StoredProcedure, spName);
        }

        public static int ExecSql(out int id, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                return ExecSql(out id, connection, commandType, commandText, commandParameters);
            }
        }

        public static int ExecSql(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return num;
        }

        public static int ExecSql(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            return num;
        }

        public static int ExecSql(out int id, DbConnection connection, CommandType commandType, string commandText)
        {
            return ExecSql(out id, connection, commandType, commandText, null);
        }

        public static int ExecSql(out int id, DbTransaction transaction, CommandType commandType, string commandText)
        {
            return ExecSql(out id, transaction, commandType, commandText, null);
        }

        public static int ExecSql(out int id, DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (Provider.GetLastIDSQL().Trim() == "")
            {
                throw new ArgumentNullException("GetLastIDSQL is \"\"");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            command.CommandType = CommandType.Text;
            command.CommandText = Provider.GetLastIDSQL();
            int.TryParse(command.ExecuteScalar().ToString(), out id);
            queryCount += 1L;
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return num;
        }

        public static int ExecSql(out int id, DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            int num = command.ExecuteNonQuery();
            command.Parameters.Clear();
            command.CommandType = CommandType.Text;
            command.CommandText = Provider.GetLastIDSQL();
            int.TryParse(command.ExecuteScalar().ToString(), out id);
            return num;
        }

        public static int ExecSqlTypedParams(string spName, DataRow dataRow)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, dataRow);
                return ExecSql(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(CommandType.StoredProcedure, spName);
        }

        public static int ExecSqlTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return ExecSql(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(connection, CommandType.StoredProcedure, spName);
        }

        public static int ExecSqlTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return ExecSql(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return ExecSql(transaction, CommandType.StoredProcedure, spName);
        }

        public static void ExecuteCommandWithSplitter(string commandText)
        {
            ExecuteCommandWithSplitter(commandText, "\r\nGO\r\n", false);
        }

        public static bool ExecuteCommandWithSplitter(string commandText, string splitter, bool errorExit = false)
        {
            int num2;
            int startIndex = 0;
            bool flag = true;
        Label_0004:
            num2 = commandText.IndexOf(splitter, startIndex);
            int length = ((num2 > startIndex) ? num2 : commandText.Length) - startIndex;
            string str = commandText.Substring(startIndex, length);
            if (str.Trim().Length > 0)
            {
                try
                {
                    ExecSql(CommandType.Text, str);
                }
                catch
                {
                    flag = false;
                    if (errorExit)
                    {
                        return flag;
                    }
                }
            }
            if (num2 != -1)
            {
                startIndex = num2 + splitter.Length;
                if (startIndex < commandText.Length)
                {
                    goto Label_0004;
                }
            }
            return flag;
        }

        public static void FillDataSet(CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                FillDataSet(connection, commandType, commandText, dataSet, tableNames);
            }
        }

        public static void FillDataSet(string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                FillDataSet(connection, spName, dataSet, tableNames, parameterValues);
            }
        }

        public static void FillDataSet(CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                FillDataSet(connection, commandType, commandText, dataSet, tableNames, commandParameters);
            }
        }

        public static void FillDataSet(DbConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            FillDataSet(connection, commandType, commandText, dataSet, tableNames, null);
        }

        public static void FillDataSet(DbConnection connection, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                FillDataSet(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, spParameterSet);
            }
            else
            {
                FillDataSet(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
            }
        }

        public static void FillDataSet(DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
        {
            FillDataSet(transaction, commandType, commandText, dataSet, tableNames, null);
        }

        public static void FillDataSet(DbTransaction transaction, string spName, DataSet dataSet, string[] tableNames, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                FillDataSet(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, spParameterSet);
            }
            else
            {
                FillDataSet(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
            }
        }

        public static void FillDataSet(DbConnection connection, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            FillDataSet(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
        }

        public static void FillDataSet(DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            FillDataSet(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
        }

        private static void FillDataSet(DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (dataSet.IsNull())
            {
                throw new ArgumentNullException("dataSet");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                if (tableNames.IsNotNull() && (tableNames.Length > 0))
                {
                    string sourceTable = "Table";
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        if (tableNames[i].IsNull() || (tableNames[i].Length == 0))
                        {
                            throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
                        }
                        adapter.TableMappings.Add(sourceTable, tableNames[i]);
                        sourceTable = sourceTable + ((i + 1)).ToString();
                    }
                }
                adapter.Fill(dataSet);
                command.Parameters.Clear();
            }
            if (mustCloseConnection)
            {
                connection.Close();
            }
        }

        public static DbParameter[] GetCachedParameterSet(string commandText)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            string str = ConnString + ":" + commandText;
            DbParameter[] parameterArray = paramCache[str] as DbParameter[];
            if (parameterArray.IsNull())
            {
                return null;
            }
            return CloneParameters(parameterArray);
        }

        public static DataSet GetDataSet(string commandText)
        {
            return GetDataSet((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public static DataSet GetDataSet(CommandType commandType, string commandText)
        {
            return GetDataSet(commandType, commandText, null);
        }

        public static DataSet GetDataSet(string commandText, params DbParameter[] commandParameters)
        {
            return GetDataSet((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public static DataSet GetDataSet(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataSet(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(CommandType.StoredProcedure, spName);
        }

        public static DataSet GetDataSet(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                return GetDataSet(connection, commandType, commandText, commandParameters);
            }
        }

        public static DataSet GetDataSet(DbConnection connection, CommandType commandType, string commandText)
        {
            return GetDataSet(connection, commandType, commandText, null);
        }

        public static DataSet GetDataSet(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataSet(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(connection, CommandType.StoredProcedure, spName);
        }

        public static DataSet GetDataSet(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return GetDataSet(transaction, commandType, commandText, null);
        }

        public static DataSet GetDataSet(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataSet(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(transaction, CommandType.StoredProcedure, spName);
        }

        public static DataSet GetDataSet(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                queryCount += 1L;
                command.Parameters.Clear();
                if (mustCloseConnection)
                {
                    connection.Close();
                }
                return dataSet;
            }
        }

        public static DataSet GetDataSet(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                command.Parameters.Clear();
                return dataSet;
            }
        }

        public static DataSet GetDataSetTypedParams(string spName, DataRow dataRow)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataSet(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(CommandType.StoredProcedure, spName);
        }

        public static DataSet GetDataSetTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataSet(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(connection, CommandType.StoredProcedure, spName);
        }

        public static DataSet GetDataSetTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataSet(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataSet(transaction, CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTable(string commandText)
        {
            return GetDataTable((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, null);
        }

        public static DataTable GetDataTable(CommandType commandType, string commandText)
        {
            return GetDataTable(commandType, commandText, null);
        }

        public static DataTable GetDataTable(string commandText, params DbParameter[] commandParameters)
        {
            return GetDataTable((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public static DataTable GetDataTable(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataTable(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTable(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                return GetDataTable(connection, commandType, commandText, commandParameters);
            }
        }

        public static DataTable GetDataTable(DbConnection connection, CommandType commandType, string commandText)
        {
            return GetDataTable(connection, commandType, commandText, null);
        }

        public static DataTable GetDataTable(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataTable(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(connection, CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTable(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return GetDataTable(transaction, commandType, commandText, null);
        }

        public static DataTable GetDataTable(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDataTable(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(transaction, CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTable(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataTable dataTable = new DataTable
                {
                    TableName = "NewTableName"
                };
                adapter.Fill(dataTable);
                queryCount += 1L;
                command.Parameters.Clear();
                if (mustCloseConnection)
                {
                    connection.Close();
                }
                return dataTable;
            }
        }

        public static DataTable GetDataTable(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.SelectCommand = command;
                DataTable dataTable = new DataTable
                {
                    TableName = "NewTableName"
                };
                adapter.Fill(dataTable);
                command.Parameters.Clear();
                return dataTable;
            }
        }

        public static DataTable GetDataTableTypedParams(string spName, DataRow dataRow)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataTable(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTableTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataTable(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(connection, CommandType.StoredProcedure, spName);
        }

        public static DataTable GetDataTableTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDataTable(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDataTable(transaction, CommandType.StoredProcedure, spName);
        }

        public static DbDataReader GetDbDataReader(string commandText)
        {
            return GetDbDataReader((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText);
        }

        public static DbDataReader GetDbDataReader(CommandType commandType, string commandText)
        {
            return GetDbDataReader(commandType, commandText, null);
        }

        public static DbDataReader GetDbDataReader(string commandText, params DbParameter[] commandParameters)
        {
            return GetDbDataReader((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public static DbDataReader GetDbDataReader(string spName, params object[] parameterValues)
        {
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDbDataReader(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDbDataReader(CommandType.StoredProcedure, spName);
        }

        public static DbDataReader GetDbDataReader(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            DbDataReader reader;
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            DbConnection connection = null;
            try
            {
                connection = Factory.CreateConnection();
                connection.ConnectionString = ConnString;
                connection.Open();
                reader = GetDbDataReader(connection, null, commandType, commandText, commandParameters, DbConnectionOwnership.Internal);
            }
            catch
            {
                if (connection.IsNotNull())
                {
                    connection.Close();
                }
                throw;
            }
            return reader;
        }

        public static DbDataReader GetDbDataReader(DbConnection connection, CommandType commandType, string commandText)
        {
            return GetDbDataReader(connection, commandType, commandText, null);
        }

        public static DbDataReader GetDbDataReader(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDbDataReader(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDbDataReader(connection, CommandType.StoredProcedure, spName);
        }

        public static DbDataReader GetDbDataReader(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return GetDbDataReader(transaction, commandType, commandText, null);
        }

        public static DbDataReader GetDbDataReader(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetDbDataReader(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDbDataReader(transaction, CommandType.StoredProcedure, spName);
        }

        public static DbDataReader GetDbDataReader(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            return GetDbDataReader(connection, null, commandType, commandText, commandParameters, DbConnectionOwnership.External);
        }

        public static DbDataReader GetDbDataReader(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            return GetDbDataReader(transaction.Connection, transaction, commandType, commandText, commandParameters, DbConnectionOwnership.External);
        }

        private static DbDataReader GetDbDataReader(DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters, DbConnectionOwnership connectionOwnership)
        {
            DbDataReader reader2;
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            bool mustCloseConnection = false;
            DbCommand command = Factory.CreateCommand();
            try
            {
                DbDataReader reader;
                PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
                if (connectionOwnership == DbConnectionOwnership.External)
                {
                    reader = command.ExecuteReader();
                }
                else
                {
                    reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                }
                queryCount += 1L;
                bool flag2 = true;
                foreach (DbParameter parameter in command.Parameters)
                {
                    if (parameter.Direction != ParameterDirection.Input)
                    {
                        flag2 = false;
                    }
                }
                if (flag2)
                {
                    command.Parameters.Clear();
                }
                reader2 = reader;
            }
            catch
            {
                connection.Close();
                throw;
            }
            return reader2;
        }

        public static DbDataReader GetDbDataReaderTypedParams(string spName, DataRow dataRow)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDbDataReader(ConnString, new object[] { CommandType.StoredProcedure, spName, spParameterSet });
            }
            return GetDbDataReader(ConnString, new object[] { CommandType.StoredProcedure, spName });
        }

        public static DbDataReader GetDbDataReaderTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDbDataReader(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDbDataReader(connection, CommandType.StoredProcedure, spName);
        }

        public static DbDataReader GetDbDataReaderTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetDbDataReader(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetDbDataReader(transaction, CommandType.StoredProcedure, spName);
        }

        public static string GetIdentifierEnd()
        {
            return Provider.GetIdentifierEnd();
        }

        public static string GetIdentifierStart()
        {
            return Provider.GetIdentifierStart();
        }

        public static string GetParamIdentifier()
        {
            return Provider.GetParamIdentifier();
        }

        public static object GetScalar(string commandText)
        {
            return GetScalar((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText);
        }

        public static object GetScalar(CommandType commandType, string commandText)
        {
            return GetScalar(commandType, commandText, null);
        }

        public static object GetScalar(string commandText, params DbParameter[] commandParameters)
        {
            return GetScalar((commandText.IndexOf(" ") > 0) ? CommandType.Text : CommandType.StoredProcedure, commandText, commandParameters);
        }

        public static object GetScalar(string spName, params object[] parameterValues)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                if (connection.IsNull())
                {
                    throw new ArgumentNullException("connection");
                }
                if (spName.IsNull() || (spName.Length == 0))
                {
                    throw new ArgumentNullException("spName");
                }
                if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
                {
                    DbParameter[] spParameterSet = GetSpParameterSet(spName);
                    AssignParameterValues(spParameterSet, parameterValues);
                    return GetScalar(connection, spName, (object[])spParameterSet);
                }
                return GetScalar(CommandType.StoredProcedure, spName);
            }
        }

        public static object GetScalar(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                connection.Open();
                return GetScalar(connection, commandType, commandText, commandParameters);
            }
        }

        public static object GetScalar(DbConnection connection, CommandType commandType, string commandText)
        {
            return GetScalar(connection, commandType, commandText, null);
        }

        public static object GetScalar(DbConnection connection, string spName, params object[] parameterValues)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetScalar(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetScalar(connection, CommandType.StoredProcedure, spName);
        }

        public static object GetScalar(DbTransaction transaction, CommandType commandType, string commandText)
        {
            return GetScalar(transaction, commandType, commandText, null);
        }

        public static object GetScalar(DbTransaction transaction, string spName, params object[] parameterValues)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (parameterValues.IsNotNull() && (parameterValues.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, parameterValues);
                return GetScalar(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetScalar(transaction, CommandType.StoredProcedure, spName);
        }

        public static object GetScalar(DbConnection connection, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, connection, null, commandType, commandText, commandParameters, out mustCloseConnection);
            object obj2 = command.ExecuteScalar();
            command.Parameters.Clear();
            if (mustCloseConnection)
            {
                connection.Close();
            }
            return obj2;
        }

        public static object GetScalar(DbTransaction transaction, CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            DbCommand command = Factory.CreateCommand();
            bool mustCloseConnection = false;
            PrepareCommand(command, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
            object obj2 = command.ExecuteScalar();
            queryCount += 1L;
            command.Parameters.Clear();
            return obj2;
        }

        public static string GetScalarToStr(CommandType commandType, string commandText)
        {
            object scalar = GetScalar(commandType, commandText);
            if (scalar.IsNull())
            {
                return "";
            }
            return scalar.ToString();
        }

        public static string GetScalarToStr(CommandType commandType, string commandText, params DbParameter[] commandParameters)
        {
            object obj2 = GetScalar(commandType, commandText, commandParameters);
            if (obj2.IsNull())
            {
                return "";
            }
            return obj2.ToString();
        }

        public static object GetScalarTypedParams(string spName, DataRow dataRow)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetScalar(CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetScalar(CommandType.StoredProcedure, spName);
        }

        public static object GetScalarTypedParams(DbConnection connection, string spName, DataRow dataRow)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetScalar(connection, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetScalar(connection, CommandType.StoredProcedure, spName);
        }

        public static object GetScalarTypedParams(DbTransaction transaction, string spName, DataRow dataRow)
        {
            if (transaction.IsNull())
            {
                throw new ArgumentNullException("transaction");
            }
            if (transaction.IsNotNull() && transaction.Connection.IsNull())
            {
                throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            if (dataRow.IsNotNull() && (dataRow.ItemArray.Length > 0))
            {
                DbParameter[] spParameterSet = GetSpParameterSet(transaction.Connection, spName);
                AssignParameterValues(spParameterSet, dataRow);
                return GetScalar(transaction, CommandType.StoredProcedure, spName, spParameterSet);
            }
            return GetScalar(transaction, CommandType.StoredProcedure, spName);
        }

        public static DbParameter[] GetSpParameterSet(string spName)
        {
            return GetSpParameterSet(spName, false);
        }

        internal static DbParameter[] GetSpParameterSet(DbConnection connection, string spName)
        {
            return GetSpParameterSet(connection, spName, false);
        }

        public static DbParameter[] GetSpParameterSet(string spName, bool includeReturnValueParameter)
        {
            if (ConnString.IsNull() || (ConnString.Length == 0))
            {
                throw new ArgumentNullException(key);
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnString;
                return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
            }
        }

        internal static DbParameter[] GetSpParameterSet(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            using (DbConnection connection2 = (DbConnection)((ICloneable)connection).Clone())
            {
                return GetSpParameterSetInternal(connection2, spName, includeReturnValueParameter);
            }
        }

        private static DbParameter[] GetSpParameterSetInternal(DbConnection connection, string spName, bool includeReturnValueParameter)
        {
            if (connection.IsNull())
            {
                throw new ArgumentNullException("connection");
            }
            if (spName.IsNull() || (spName.Length == 0))
            {
                throw new ArgumentNullException("spName");
            }
            string str = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
            DbParameter[] parameterArray = paramCache[str] as DbParameter[];
            if (parameterArray.IsNull())
            {
                DbParameter[] parameterArray2 = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
                paramCache[str] = parameterArray2;
                parameterArray = parameterArray2;
            }
            return CloneParameters(parameterArray);
        }

        public static DbParameter MakeInParam(string ParamName, DbType DbType, int Size, object Value)
        {
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
        }

        public static DbParameter MakeOutParam(string ParamName, DbType DbType, int Size)
        {
            return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
        }

        public static DbParameter MakeInOutParam(string ParamName, DbType DbType, int Size, object Value)
        {
            return MakeParam(ParamName, DbType, Size, ParameterDirection.InputOutput, Value);
        }

        public static DbParameter MakeParam(string ParamName, DbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            DbParameter parameter = Provider.MakeParam(ParamName, DbType, Size);
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || !Value.IsNull())
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        public static Database Pool()
        {
            int count = pool.Count;
            if ((count != poolkey.Count) || (count == 0))
            {
                pool.Clear();
                poolkey.Clear();
                count = 0;
                foreach (ConnectionStringSettings settings in WebConfig.GetConn())
                {
                    string name = settings.Name;
                    if (!(name == "LocalSqlServer"))
                    {
                        Database database = new Database(name)
                        {
                            Pool = name
                        };
                        pool[name.ToLower()] = database;
                        poolkey.Add(name.ToLower());
                        count++;
                    }
                }
            }
            return pool[poolkey[(count == 1) ? 0 : Rand.RndInt(0, count)]];
        }

        public static Database Pool(string key)
        {
            key = key.ToLower();
            if (pool.ContainsKey(key))
            {
                return pool[key];
            }
            return Pool();
        }

        private static void PrepareCommand(DbCommand command, DbConnection connection, DbTransaction transaction, CommandType commandType, string commandText, DbParameter[] commandParameters, out bool mustCloseConnection)
        {
            if (command.IsNull())
            {
                throw new ArgumentNullException("command");
            }
            if (commandText.IsNull() || (commandText.Length == 0))
            {
                throw new ArgumentNullException("commandText");
            }
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }

            mustCloseConnection = true;

            command.Connection = connection;
            command.CommandText = commandText;
            if (transaction.IsNotNull())
            {
                if (transaction.Connection.IsNull())
                {
                    throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
                }
                command.Transaction = transaction;
            }
            command.CommandType = commandType;
            if (commandParameters.IsNotNull())
            {
                AttachParameters(command, commandParameters);
            }
        }

        public static void ResetDbProvider()
        {
            connString = null;
            factory = null;
            provider = null;
            dbType = null;
        }

        public static void UpdateDataSet(DbCommand insertCommand, DbCommand deleteCommand, DbCommand updateCommand, DataSet dataSet, string tableName)
        {
            if (insertCommand.IsNull())
            {
                throw new ArgumentNullException("insertCommand");
            }
            if (deleteCommand.IsNull())
            {
                throw new ArgumentNullException("deleteCommand");
            }
            if (updateCommand.IsNull())
            {
                throw new ArgumentNullException("updateCommand");
            }
            if (tableName.IsNull() || (tableName.Length == 0))
            {
                throw new ArgumentNullException("tableName");
            }
            using (DbDataAdapter adapter = Factory.CreateDataAdapter())
            {
                adapter.UpdateCommand = updateCommand;
                adapter.InsertCommand = insertCommand;
                adapter.DeleteCommand = deleteCommand;
                adapter.Update(dataSet, tableName);
                dataSet.AcceptChanges();
            }
        }

        public static Database UsePool(params string[] keys)
        {
            int count = pool.Count;
            if ((count != poolkey.Count) || (count == 0))
            {
                pool.Clear();
                poolkey.Clear();
                count = 0;
                foreach (ConnectionStringSettings settings in WebConfig.GetConn())
                {
                    string name = settings.Name;
                    if (!(name == "LocalSqlServer") && (keys.IndexOf<string>(name) != -1))
                    {
                        Database database = new Database(name)
                        {
                            Pool = name
                        };
                        pool[name.ToLower()] = database;
                        poolkey.Add(name.ToLower());
                        count++;
                    }
                }
            }
            return pool[poolkey[(count == 1) ? 0 : Rand.RndInt(0, count)]];
        }

        public static string ConnString
        {
            get
            {
                if (string.IsNullOrEmpty(connString) && ConfigurationManager.ConnectionStrings[key].IsNotNull())
                {
                    accessType = ConfigurationManager.AppSettings["accessType"];
                    pwKey = ConfigurationManager.AppSettings["Key"];
                    connString = ConfigurationManager.ConnectionStrings[key].ToString();
                    //if (accessType == "c")
                    //{
                    //client客户端访问对连接字符串解密
                    connString = EncryptExtensions.DESDecode(connString, pwKey);
                    //}
                    dbType = ConfigurationManager.ConnectionStrings[key].ProviderName;
                }
                return connString;
            }
            set
            {
                connString = value;
            }
        }

        public static string DBType
        {
            get
            {
                if (string.IsNullOrEmpty(dbType))
                {
                    if (ConfigurationManager.ConnectionStrings[key].IsNotNull())
                    {
                        dbType = ConfigurationManager.ConnectionStrings[key].ProviderName;
                    }
                    else
                    {
                        dbType = "SqlServer";
                    }
                }
                return dbType;
            }
            set
            {
                dbType = value;
            }
        }

        public static DbProviderFactory Factory
        {
            get
            {
                if (factory.IsNull())
                {
                    factory = Provider.Instance();
                }
                return factory;
            }
        }

        public static string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
                ResetDbProvider();
            }
        }

        public static IDbProvider Provider
        {
            get
            {
                if (provider.IsNull())
                {
                    lock (lockHelper)
                    {
                        if (provider.IsNull())
                        {
                            dbType = DBType;
                            try
                            {
                                provider = (IDbProvider)"Pub.Class.{0},Pub.Class.{0}".FormatWith(dbType).LoadClass();
                            }
                            catch
                            {
                                throw new Exception(dbType + " - 请检查web.config中DbType节点数据库类型是否正确，例如：SqlServer、Access、MySql、OleDb、Odbc");
                            }
                        }
                    }
                }
                return provider;
            }
        }

        public static long QueryCount
        {
            get
            {
                return queryCount;
            }
            set
            {
                queryCount = value;
            }
        }

        private enum DbConnectionOwnership
        {
            Internal,
            External
        }

    }
}

